import server from '@/utils/config.js'

export default class Request {
    http(param) {
        // 请求参数
        var url = param.url,
            method = param.method,
            header = {},
            data = param.data || {},
            token = param.token || "",
            hideLoading = param.hideLoading || false;

        //拼接完整请求地址
        var requestUrl = server.api + url;
       //拼接完整请求地址（根据环境切换）
       // var requestUrl = server.api() + url;

        //请求方式:GET或POST(POST需配置
        // header: {'content-type' : "application/x-www-form-urlencoded"},)
        if(param.url == '/uploadFile'){
/* 			header = {
				'Content-Type': 'multipart/form-data'
			}; */
		}else if (method) {
            method = method.toUpperCase(); //小写改为大写
            if (method == "POST" ) {
                if(param.header && param.header['Content-Type']){
                    console.log(param.header['Content-Type'])
                    header = {
						'Content-Type': param.header['Content-Type']
					};
                }else{
                    header = {'Content-Type': "application/json"
                                };
                }
                
            } else {
                header = {
                    'Content-Type': "application/json",
                };
            }
        }
        
		if(uni.getStorageSync("token")){
			header['Authorization'] = `Bearer ${uni.getStorageSync("token")}`;
		}
        //加载圈
        if (!hideLoading) {
            uni.showLoading({
                title: '加载中...'
            });
        }
        // 返回promise
        return new Promise((resolve, reject) => {
            // 请求
            uni.request({
                url: requestUrl,
                data: data,
                method: method,
                header: header,
                success: (res) => {
                    // 判断 请求api 格式是否正确
					console.log(res)
					if(res && res.statusCode === 401){
						// uni.showToast({
						//     title: "api错误" + res.data.msg,
						//     icon: 'none'
						// });
						
						uni.redirectTo({
							url: '/pages/login/login'
						})
					}else if (res.statusCode && res.statusCode != 200) {
						   // debugger
                        uni.showToast({
                            title: "api错误" + res.errMsg,
                            icon: 'none'
                        });
                    }
					if (res.header['authorization'] != null && res.header['authorization'] != undefined && res.header['authorization'] != "") {
					    uni.setStorageSync("token", res.header["authorization"])
					} else if (res.header['Authorization'] != null && res.header['Authorization'] != undefined && res.header['Authorization'] != "") {
					    uni.setStorageSync("token", res.header["Authorization"])
					}
                    // 将结果抛出
                    resolve(res.data)
                },
                //请求失败
                fail: (e) => {
                    uni.showToast({
                        title: "" + e.data.msg,
                        icon: 'none'
                    });
                    resolve(e.data);
                },
                //请求完成
                complete() {
                    //隐藏加载
                    if (!hideLoading) {
                        uni.hideLoading();
                    }
                    resolve();
                    return;
                }
            })
        })
    }
}